from tkinter import Frame, Label, Button
from tkinter import W,E,S,N
from tkinter import END

from CustomWidget.custom_button import CustomLabelCopyButtonPair
from CustomWidget.custom_label_pair import CustomLabelCheckbox, CustomLabelPairEdit, CustomLabelText
from CommonOperations.parse_img import *
from CommonOperations.get_all_configs import get_all_configs

class CreateNewImageFrame(Frame):
    def __init__(self, master):
        self.root = master
        self.root.config()
        self.root.title("创建图片")
        # self.root.geometry("1000x800")

        super(CreateNewImageFrame, self).__init__(master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        """
        新建图片窗口_页面控件
        """
        # 菜单LabelButton控件
        self.create_menu = Label(self, text="创建图片", justify='center', width=25, background='#F5F5F5')
        self.create_menu.grid(row=1, column=3, sticky=W, columnspan=2)
        self.back_history_button = Button(self, text="历史记录", command=self.change_history_frame, width=25)
        self.back_history_button.grid(row=1, column=1, sticky=W, columnspan=2)

        # 主页面
        all_ini_data = get_all_configs()
        # 单独的设置按钮
        self.back_history_button = Button(self, text="设置", command=self.change_setting_frame, width=10)
        self.back_history_button.grid(row=2, column=4, sticky=W, columnspan=1)

        self.whether_timestamp = CustomLabelCheckbox(self, "日期", 2, check_num=1, label_data=all_ini_data['timestamp'])
        self.img_w = CustomLabelPairEdit(self, "W 宽px", 3, c=1, right_col=1, label_data=all_ini_data['img_w'])
        self.img_h = CustomLabelPairEdit(self, "H 高px", 3, c=3, right_col=1, label_data=all_ini_data['img_h'])
        self.img_type = CustomLabelCheckbox(self, "格式", 4, check_num=3, label_data=all_ini_data['check_list'])
        self.whether_add_text = CustomLabelCheckbox(self, "文字水印", 5, check_num=1, label_data=all_ini_data['whether_add_text'])
        self.whether_add_border = CustomLabelCheckbox(self, "图片描边", 6, check_num=1, label_data=all_ini_data['whether_add_border'])
        self.desc_text = CustomLabelText(self, "说明", 7)
        self.create_img_button = Button(self, text="生成填充图片", command=self.click_create_img, justify='center', width=40)
        self.create_img_button.grid(row=10, column=1, columnspan=4, pady=10)

        # 复制图片路径
        self.copy_jpg = CustomLabelCopyButtonPair(self, 20, "jpg复制")
        self.copy_png = CustomLabelCopyButtonPair(self, 22, "png复制")
        self.copy_gif = CustomLabelCopyButtonPair(self, 24, "gif复制")

    def click_create_img(self):
        """生成填充图片"""
        try:
            img_width = int(self.img_w.right_input.get())
        except:
            img_width = 0
        try:
            img_height = int(self.img_h.right_input.get())
        except:
            img_height = 0
        whether_add_text = self.whether_add_text.v1.get()
        whether_add_border = self.whether_add_border.v1.get()
        desc_text = self.desc_text.right_text.get(index1=0.0, index2=END)
        font_color = get_all_configs()['font_color']
        font_size = int(get_all_configs()['font_size'])
        whether_time = self.whether_timestamp.v1.get()
        whether_jpg = self.img_type.v1.get()
        whether_png = self.img_type.v2.get()
        whether_gif = self.img_type.v3.get()
        img_dir = get_all_configs()['small_dir']
        jpg_name = png_name = gif_name = ""
        if whether_jpg:
            jpg_name = create_img(img_width,img_height,whether_add_text,font_color,font_size,desc_text,whether_time,"JPG",img_dir,whether_add_border)
        if whether_png:
            png_name = create_img(img_width,img_height,whether_add_text,font_color,font_size,desc_text,whether_time,"PNG",img_dir,whether_add_border)
        if whether_gif:
            gif_name = create_img(img_width,img_height,whether_add_text,font_color,font_size,desc_text,whether_time,"GIF",img_dir,whether_add_border)

        img_list = {"JPG": jpg_name,
                    "PNG": png_name,
                    "GIF": gif_name}
        self.copy_jpg.left_label['text'] = img_list['JPG']
        self.copy_png.left_label['text'] = img_list['PNG']
        self.copy_gif.left_label['text'] = img_list['GIF']

    def change_history_frame(self):
        """切换历史记录窗口"""
        self.destroy()
        from PageFrames.history_frame import HistoryFrame

        HistoryFrame(self.root)

    def change_setting_frame(self):
        """切换设置窗口"""
        self.destroy()
        from PageFrames.setting_frame import SettingFrame

        SettingFrame(self.root)

